package com.gitee.melin.bee.core.hibernate5.type;

import org.apache.commons.lang3.StringUtils;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.Arrays;
import java.util.List;

@Converter
public class StringListConverter implements AttributeConverter<List<String>, String> {

    @Override
    public String convertToDatabaseColumn(List<String> list) {
        if (list == null) {
            return null;
        } else {
            return String.join(",", list);
        }
    }

    @Override
    public List<String> convertToEntityAttribute(String joined) {
        if (StringUtils.isNotBlank(joined)) {
            return Arrays.asList(joined.split(","));
        } else {
            return null;
        }
    }

}
